package day12.console.impl;

import day12.console.ConsoleCommand;
import day12.packet.request.CreateGroupRequestPacket;
import day12.util.session.SessionUtil;
import io.netty.channel.Channel;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @author chenshijie
 * @date 2024/1/1 14:17
 */

public class CreateGroupConsoleCommand implements ConsoleCommand {
    private static final String USER_ID_SPLIT = ",";
    @Override
    public void exec(Scanner sc, Channel channel) {
        CreateGroupRequestPacket createGroupRequestPacket = new CreateGroupRequestPacket();

        System.out.println("【拉人群聊】输入userId列表，userId之间英文逗号隔开: ");
        String userIds = sc.nextLine();
        List<Integer> userIdsList = new ArrayList<>();
        int groupBossUserId = SessionUtil.getSession(channel).getUserId();
        userIdsList.add(groupBossUserId);
        for (String userIdVal : userIds.split(USER_ID_SPLIT)) {
            int userId = Integer.parseInt(userIdVal);
            if (groupBossUserId == userId) {
                continue;
            }
            userIdsList.add(userId);
        }
        createGroupRequestPacket.setUserIdsList(userIdsList);

        channel.writeAndFlush(createGroupRequestPacket);
    }
}
